package UPPM;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.openqa.selenium.support.ui.Select;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;

import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;

public class TestProjects 
{
		
	WebDriver driver = new FirefoxDriver();
	
	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.manage().deleteAllCookies();
		driver.get("http://test.unippmtest.co.uk");	
		}

	
	
	@Test
	public void testProjectsUiInitialState() throws Exception {

		Utils.Login(driver);
		
		Thread.sleep(1000);
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebElement projects =  driver.findElement(By.xpath("/html/body/div[7]/div/div/ul/li[4]/a"));
		
		Actions builder = new Actions(driver);
		builder.click(projects).perform();
		
		Utils.waitForElement(driver, By.xpath(".//*[@id='manage_Buddies']/a"));
		
		WebElement selected_project = driver.findElement(By.xpath(".//*[@id='tbrgt']/div[1]"));
		Assert.assertEquals("Selected Project", selected_project.getText().trim());
		WebElement add_proj = driver.findElement(By.xpath(".//*[@id='pageRows']/div/div[1]/div[1]/div/div[1]/div/div[2]/div[1]/div[1]/a"));
		Assert.assertEquals("Add a new project", add_proj.getText().trim());
		WebElement sort = driver.findElement(By.id("sort"));
		Assert.assertEquals(true, sort.isDisplayed());
		WebElement results_per_page = driver.findElement(By.id("projects_results_num"));
		Assert.assertEquals(true, results_per_page.isDisplayed());
		
		}
	
	@Test
	public void testBrowseMiniProjectsTab() throws Exception {
		
		Utils.Login(driver);
		
		Thread.sleep(3000);
//		driver.findElement(By.linkText("Browse Music")).click();
		WebElement browse_music = driver.findElement(By.xpath(".//*[@id='sub-nav-menu']/li[1]/a"));
		Thread.sleep(3000);
		Actions builder = new Actions(driver);
		builder.click(browse_music).perform();
		
		Thread.sleep(3000);
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		Utils.waitForElement(driver, By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div/div/div/a"));
		WebElement proj_explorer = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div/div/div[2]/a"));
		Thread.sleep(1000);
		Actions builder2 = new Actions(driver);
		builder2.click(proj_explorer).perform();
		
		
//		Assert.assertEquals("Project Explorer", proj_explorer.getText().trim());
		Utils.waitForElement(driver, By.xpath(".//*[@id='explorer']/div/div/div[1]/div[1]/div[1]/a"));
		WebElement add_proj= driver.findElement(By.cssSelector(".ins>a"));
		Assert.assertEquals("Add a new project", add_proj.getText().trim());
		
		WebElement details= driver.findElement(By.xpath(".//*[@id='explorer']/div/div/div[1]/div[1]/div[2]/a"));
		Assert.assertEquals("Project Details", details.getText().trim());
		WebElement expand_all= driver.findElement(By.xpath(".//*[@id='expand']/span"));
		Assert.assertEquals(true, expand_all.isDisplayed());
		WebElement deselect_all= driver.findElement(By.xpath(".//*[@id='explorer']/div/div/div[2]/a[3]/span"));
		Assert.assertEquals(true, deselect_all.isDisplayed());
		
}

	
	@Test
	public void testAddNewProject() throws Exception {
		Utils.Login(driver);
	
		Thread.sleep(3000);
//		driver.findElement(By.linkText("Browse Music")).click();
		WebElement browse_music = driver.findElement(By.xpath(".//*[@id='sub-nav-menu']/li[1]/a"));
		Thread.sleep(1000);
		Actions builder = new Actions(driver);
		builder.click(browse_music).perform();
		
		Thread.sleep(1000);
		
		driver.findElement(By.linkText("Project Explorer")).click();
//		driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div/div/div[2]/a")).click();
//		Actions builder2 = new Actions(driver);
//		builder2.click(proj_explorer).perform();
		
		
		WebElement add_new = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div/div[2]/div[2]/div/div/div/div/div/a"));
		add_new.click();
		Utils.waitForElement(driver, By.id("addnewpjt"));
		WebElement new_proj = driver.findElement(By.id("newproject"));
		new_proj.sendKeys("Test new project");
		WebElement add_btn = driver.findElement(By.id("addnewpjt"));
		add_btn.click();
		//wait for page to load!!!!!!
		Thread.sleep(1000);
		
		WebElement added_proj = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[3]/div/div/div/div/div/div/div[2]/a/span/span[2]"));
		Assert.assertEquals("Test new project", added_proj.getText());
	}
	
	
	@After
	public void tearDown() throws Exception {
		System.out.println("******************************");
//		File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//		FileUtils.copyFile(scrFile, new File("c:\\users\\Public\\screenshot.png"));
//		BufferedWriter out = new BufferedWriter(new FileWriter("c:\\users\\Public\\test.txt"));
//		out.write(driver.getPageSource());
//		Thread.sleep(5000);
		driver.quit();
	}
	
}

